
package com.ltg.topactivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import java.util.Random;

public class ScaleListView extends ListView {

    private Random random = new Random();
    private float degree = 1.3f;
    private Paint paint;

    public ScaleListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPaint();
    }

    public ScaleListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public ScaleListView(Context context) {
        super(context);
        initPaint();
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(0xFF55CCFF);
        paint.setStyle(Style.FILL);
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        Rect r = new Rect(child.getLeft(), child.getTop(), child.getRight(),
                child.getBottom());
        paint.setColor(0xFFFFFFFF & random.nextInt() | 0xFF000000);
        // canvas.drawRect(r, paint);
        int count = canvas.save();
        Matrix matrix = new Matrix();
        int centerY = getHeight() / 2;
        if (child.getTop() <= centerY && child.getBottom() >= centerY) {
            float scale = degree
                    - (child.getBottom() - child.getHeight() / 2 - centerY) * 2
                    / child.getHeight();
            Log.e("scale", "" + scale);
            matrix.setScale(scale, scale, child.getRight() - child.getWidth()
                    / 2, child.getBottom() - child.getHeight() / 2);
        }
        canvas.setMatrix(matrix);

        boolean drawChild = super.drawChild(canvas, child, drawingTime);
        canvas.restoreToCount(count);
        return drawChild;
    }

}
